<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">

</head>
<body>
<div class="panel panel-default operation">
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <input name="contentTitle" hidden/>
            <input name="contentId" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-inline">
                    <select id="organType" name="organType" lay-filter="organType">
                        <!--<option value="">选择简介类型</option>-->
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block"style="width: 90%; height: 100%">
                    <textarea id="content" name=content >内容</textarea>
                </div>

            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <!--<button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>-->
                </div>
            </div>
        </form>
    </div>
</div>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = encodeURIComponent(token);
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var layedit = layui.layedit;

    layui.config({
        base: '/'
    }).extend({
        tinymce: 'layui_exts/tinymce/tinymce'
    }).use(['layer', 'layedit', 'tinymce'], function () {
        var tinymce = layui.tinymce;
        var content = tinymce.render(
            {
                elem: "#content"
                , height: 600
                , images_upload_url: '/sysContent/picture/upload?authorization=' + tokenQuery
            }
            ,function (){
                // 加载完成

            }
        );
        // layer.ready(function () {
        // });
        organType();
        function organType(){
            var send = {name:"organMsgType"}
            CoreUtil.sendPost(
                "/sysDict/listByPage",
                send,
                function(result){
                    var data = result.data;
                    organTypes(data.records[0].id);
                }
            );
        }
        function organTypes(dictId){
            var send = {dictId:dictId}
            CoreUtil.sendPost(
                "/sysDictDetail/listByPage",
                send,
                function(result){
                    console.log(result);
                    var data = result.data;
                    for(var i = 0 ; i < data.records.length; i ++){
                        $("#organType").append("<option value=\""+ data.records[i].value +"\">"+ data.records[i].label +"</option>");
                        // $('#organType').append(new Option(data.records[i].label,data.records[i].value));//往下拉菜单里添加元素
                        if(i == 0 ){
                            getContent(data.records[i].value);
                        }
                    }
                    form.render("select")
                }
            );
        }
        // organType

        form.on('select(organType)',function (data) {
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            if(data.value != ""){
                getContent(data.value);
            }
        });


        // //图片
        // layedit.set({
        //     uploadImage: {
        //         url: '/sysContent/picture/upload?authorization=' + tokenQuery //接口url
        //         , type: 'post' //默认post
        //         ,size: 1024
        //     }
        // });
        // //建立富文本编辑器
        // var editIndex = layedit.build('content',{width:"90%",height:400});

        function getContent(type){
            CoreUtil.sendGet(
                "/sysOrganMessage/"+type,
                null,
                function(result){
                    console.log(result);
                    var data = result.data;
                    if(null == data){
                        // layedit.setContent(editIndex, "");
                        tinymce.get('#content').setContent('');
                        $(".operation input[name=id]").val("");
                        $(".operation input[name=contentId]").val("");
                        $(".operation input[name=contentTitle]").val("");
                    }else{
                        // layedit.setContent(editIndex, data.content);
                        tinymce.get('#content').setContent(data.content);
                        $(".operation input[name=id]").val(data.id);
                        $(".operation input[name=contentId]").val(data.contentId);
                        $(".operation input[name=contentTitle]").val(data.contentTitle);
                    }

                }
            );
        }
        //监听保存
        form.on('submit(submit)', function (data) {
            data.field.content = tinymce.get('#content').getContent();
            console.log(data.field)
            var send = {
                content: data.field.content,
                type : data.field.organType,
                // type : data.field.organType
            }
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/sysOrganMessage/add/", send, function (res) {
                    // $(".table_div").show();
                    // $(".operation").hide();
                    layer.msg("修改成功")
                })
            } else {
                CoreUtil.sendPut("/sysOrganMessage/update", data.field, function (res) {
                    // $(".table_div").show();
                    // $(".operation").hide();
                    layer.msg("修改成功")
                })
            }
            return false;
        });
    });

</script>